<?php
/**
 * Created by PhpStorm.
 * Author: i@fedax.cn
 * Date: 2018/1/18
 * Time: 17:39
 */

namespace app\admin\controller;

use think\facade\Env;
use app\admin\model\AppManage as AppManageModel;
use app\admin\model\OperateLog as GoldEggPrizeLogModel;
/**
 * APP版本管理
 * Class AppManage
 * @package app\admin\controller
 */
class AppManage extends Index
{
    public function index(){
        return view();
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function indexData(){
        $appManageModel = new AppManageModel();
        $page_config = array(
            'page' => input('post.offset', 1),
            'list_rows' => input('post.limit', 10),
        );
        $data = $appManageModel->getDataList($page_config);
        return json($data);
    }


    /**
     * @return string
     * @throws \OSS\Core\OssException
     */
    public function getApp()
    {
        $file = request()->file('file');
        $info = $file->rule('uniqid')->move(Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'upload');
        // 保存目录
        $path = Env::get('root_path') . 'public' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . $info->getSaveName();//本地文件路径
        $object_name = 'android/' . $info->getSaveName();


        $data_ = 'AdminToken=' . urlencode(session('admin_token'));
        $url = config('api_url') . '/?s=App.AppManage.getVerifyToken';
        $apiReturn = json_decode(curl($url, $data_), true);
        if ($apiReturn['ret'] == 200) {
            $ossClient = new \OSS\OssClient($apiReturn['data']['AccessKeyId'], $apiReturn['data']['AccessKeySecret'], $apiReturn['data']['EndPoint'], false, $apiReturn['data']['SecurityToken']);
            try {
                $ossClient->uploadFile($apiReturn['data']['Bucket'], $object_name, $path);
                return $object_name;
            } catch (\OSS\Core\OssException $e) {
                exception($e->getMessage());
            }
        }
        return $object_name;
    }

    /**
     * @return \think\response\View
     * 添加
     */
    public function add()
    {
        $goldEggPrizeLogModel = new GoldEggPrizeLogModel();
        if (request()->isPost()) {
            $data = array();
            $data['versionCode'] = input('versionCode');
            $data['versionName'] = input('versionName');
            $data['up_desc'] = input('up_desc');
            $data['down_url'] = input('down_url');
            $validate = validate('AppManage');
            if (!$validate->scene('add')->check($data)) {
                $this->error($validate->getError());
            };
            $data_ = 'AdminToken=' . urlencode(session('admin_token'));
            $data_ .= '&versionCode=' . urlencode(input('versionCode'));
            $data_ .= '&versionName=' . urlencode(input('versionName'));
            $data_ .= '&up_desc=' . urlencode(input('up_desc'));
            $data_ .= '&down_url=' . urlencode(input('down_url'));
            $url = config('api_url') . '/?s=App.AppManage.Insert';
            $returnData = json_decode(curl($url, $data_), true);

            $logData['admin_id'] = session('admin_id');          //日志纪录
            $logData['content']  = session('admin_name').'添加了【'.input('versionName').input('versionCode').'】'; //日志纪录
            if ($returnData['ret'] == 200) {
                $goldEggPrizeLogModel->save($logData);
                $this->success(lang('add') . lang('success'));
            }
            $this->error(lang('add') . lang('false') . '-' . $returnData['msg']);
        }
        return redirect('/Miss');
    }

    /**
     * 删除
     * @return \think\response\Redirect
     * @throws \think\exception\PDOException
     */
    public function del()
    {
        if (request()->isPost()) {
            $appManageModel = new AppManageModel();
            if ($appManageModel->where('id', input('id'))->delete()) {
                $this->success(lang('delete') . lang('success'));
            }
            $this->error(lang('delete') . lang('false'));
        }
        return redirect('/Miss');
    }

    /**
     * 编辑
     */
    public function edit()
    {

        $appManageModel = new AppManageModel();
        $new_data['status'] = input('status');
        $new_data['is_open'] = input('is_open');


        $res = $appManageModel->save($new_data, ['id' => input('id')]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }
}